const chalk = require('chalk');
const { wechatAPIS } = require('../tools/wechatApi')
const {
    Mysql,
    priceList,
    TripleDecrypt
} = require('../tools');
module.exports = async ctx => {
    let {
        payInfo: requestBody
    } = ctx.request.body;
    try {
        let result = JSON.parse(TripleDecrypt(requestBody));
        let {
            orderId,
            openId,
            payAmount,
            uid,
            payInfo
        } = result;
        let payStatus = await wechatAPIS.orderQuery(orderId);
        if (payStatus.trade_state == 'SUCCESS') {
            let searchSql = `SELECT * FROM orderList WHERE orderId='${orderId}'`;
            let searchRes = await Mysql(searchSql);
            if (searchRes.length) {
                ctx.state.data = {
                    code: '002',
                    msg: '非法操作'
                };
                return;
            }
            else {
                let openDay = priceList.find(item => item.price == Math.round(payAmount / 100)).time;
                let sql = `INSERT INTO orderList(uid,type,orderId,openId,money,payInfo) VALUES(${uid},'VIP','${orderId}','${openId}',${Number(payAmount)},'${payInfo}')`;
                let res = await Mysql(sql);
                if (res.affectedRows == 1) {
                    let currTimeStamp = Date.now(),
                        startTime = currTimeStamp,
                        dayTimeStamp = 1000 * 60 * 60 * 24 * openDay,
                        endTime = startTime + dayTimeStamp;
                    let sql2 = `SELECT insertToVip('${openId}',${uid},${startTime},${endTime},${Number(payAmount)},'${orderId}',${currTimeStamp},${dayTimeStamp})`;
                    let res2 = await Mysql(sql2);
                    ctx.state.data = {
                        code: '001',
                        data: Object.values(res2[0])[0]
                    }
                } else {
                    ctx.state.data = {
                        code: '002',
                        msg: '内部错误'
                    }
                }
            }
        }
        else {
            ctx.state.data = {
                code: '002',
                msg: '非法操作'
            }
        }
    } catch (error) {
        ctx.state.data = {
            code: '002',
            msg: '内部错误'
        }
    }

    /* else {
       // 查询用户付款情况
       let paymentAmount = 3000;
       let currPriceList = priceList.find((item) => item.price == paymentAmount); //用户购买的当前售价
       let buyTime = currPriceList.time * 1000 * 60 * 60 * 24;
       let startTime = Date.now();
       let endTime = startTime + buyTime;
   } */
}

/*
##设置会员
DELIMITER $$
DROP FUNCTION IF EXISTS insertToVip$$
CREATE FUNCTION insertToVip(p_openId VARCHAR(30),p_uid INT,p_startTime BIGINT, p_endTime BIGINT, p_money INT, p_orderId VARCHAR(30),curr_timestamp BIGINT,dayTimeStamp BIGINT) RETURNS INT
BEGIN
  DECLARE num INT;
	DECLARE vipEndTime BIGINT;
    SELECT count(*) INTO num FROM vip where openId=p_openId;
    IF(num=0)
        THEN
        INSERT INTO vip(uid,openId,startTime,endTime,isVip,money,orderId) VALUES(p_uid,p_openId,p_startTime,p_endTime,1,p_money,p_orderId);
    ELSE
				SELECT endTime INTO vipEndTime FROM vip where openId=p_openId;
				IF(curr_timestamp<vipEndTime)
						THEN
						UPDATE vip SET endTime = vipEndTime+dayTimeStamp WHERE openId=p_openId;
				ELSE
						UPDATE vip SET endTime = p_endTime WHERE openId=p_openId;
				END IF;
  END IF;
	RETURN 1;
END $$
DELIMITER
*/